package cn.xmkeshe.back.impl;

import cn.xmkeshe.back.ICarsServiceBack;
import cn.xmkeshe.dao.ICarsDao;
import cn.xmkeshe.model.Cars;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class ICarsServiceBackImpl implements ICarsServiceBack {

  @Resource
  private ICarsDao iCarsDao;

  @Override
  public Cars findById(String carid) {
    return iCarsDao.findById(carid);
  }

  @Override
  @Transactional(propagation = Propagation.REQUIRED)
  public boolean edit(String carid,int status) {
    return iCarsDao.doUpdateByCarid(carid,status);
  }

  @Override
  @Transactional(propagation = Propagation.REQUIRED)
  public boolean add(Cars cars) {

    return iCarsDao.doCreate(cars);
  }

  @Override
  @Transactional(readOnly = true)
  public Map<String, Object> listSplit(int currentPage, int lineSize) {
    Map<String,Object> result = new HashMap<String, Object>();
    result.put("start",(currentPage-1)*lineSize);
    result.put("lineSize",lineSize);
    Map<String,Object> map = new HashMap<String, Object>();
    map.put("allCars",iCarsDao.findAllBySplit(result));
    map.put("allCount",iCarsDao.getAllCount());
    return map;
  }

  @Override
  @Transactional(readOnly = true)
  public Map<String, Object> findByNumber(int currentPage, int lineSize, String number) {
    Map<String,Object> map = new HashMap<String, Object>();
    map.put("data",iCarsDao.findByNumber((currentPage-1)*lineSize, lineSize,number));
    map.put("count",iCarsDao.getAllCount());
    return map;
  }
}
